import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:mall/core/models/user_renew_list.dart';
import 'package:mall/core/services/api.dart';
import 'package:mall/core/providers/view_model.dart';

class UserRenewListModel extends ViewModel {
  final Api api;
  final int userId;
  String _businessType = ''; // 非null默认值
  String _errorMessage;
  UserRenewList _userRenewList;

  UserRenewListModel(this.api, this.userId) : super();

  set businessType(String value) {
    debugPrint('$this set businessType = $value');
    if (_businessType != value) {
      _businessType = value;
      _initData();
    }
  }

  String get errorMessage => _errorMessage;

  UserRenewList get userRenewList => _userRenewList;

  Future<ViewState> _initData() async {
    viewState = ViewState.busy;
    try {
      _userRenewList = await api.userRenewList(userId, _businessType);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
